var RtuMarkers = [];
var RtuPoints, RtuTimer;
var RtuInfowindow ;
var rtuContent;
function GetRtu(){           
	// first time
	//alert("HelloRTU");
	$.ajax({
		url: "soapRtu.php",
		success : function (data) {            
			RtuPoints = $.parseJSON(data);
			OverlayRtu(RtuPoints); 
		}
	});
	RtuTimer = setInterval(function(){                 
		$.ajax({          
			url: "soapRtu.php",
			success : function (data) {            
				RtuPoints = $.parseJSON(data);
				OverlayRtu(RtuPoints); 
			}
		});
	},900000); // refresh user count every 15 minutes
}
function OverlayRtu(points){
	ClearOverlay_BY_JIK(RtuMarkers);
	//console.log("refreshed");
	$.each(points, function(key,data) {
		var name = data.rtu_name;
		var lat = data.rtu_lat;
		var lon = data.rtu_lon;
		var pressure = data.rtu_pressure;
		var flow = data.rtu_flow;
		var myLatlng = new google.maps.LatLng(lat,lon);
		
		 var mapLabel = new MapLabel({
	         text: pressure,
	         position: myLatlng,
	         map: map,
	         fontColor: '#ff0000',
	         strokeColor: '#00FFFF',
	         fontSize: 15
		 });
		// Different pin for different Rtu box
		// Uxxx use rtu1.png
		// Pxxx use rtu2.png
		var pattern = /(^U)/;
		if (pattern.test(name))
			var icon = "./image/rtu1.png";
		else	
			var icon = "./image/rtu2.png";
		var marker = new google.maps.Marker({
			position: myLatlng,
			map: map,
			title: name,
			icon: icon
		});
				
		mapLabel.set('position', myLatlng);	       
        marker.bindTo('map', mapLabel);
        marker.bindTo('position', mapLabel);	           	      		
        RtuMarkers.push(marker);
        
        google.maps.event.addListener(marker, 'click', function() {
			if (RtuInfowindow){
				RtuInfowindow.close();
			}
			rtuContent = "<div class='datagrid'><table><thead><tr><th>StationName</th><th>Flow</th><th>Pressure</th></tr></thead><tbody><tr><td>"+name+"</td><td>"+flow+"</td><td>"+pressure+"</td></tr></tbody></table></div>";
			RtuInfowindow = new google.maps.InfoWindow({
				content : rtuContent,
				maxWidth: 1000,
				maxHeigth: 1000							 
			});	
			RtuInfowindow.open(map, this);						
		});
	
	
	/*	var content = "<h3>" + name + "</h3>"
			+"<b>Pressure: </b>"+ pressure
			+"</br><b>Flow: </b>" + flow;
		var infowindow = new google.maps.InfoWindow({                    
				content : content
		}); 
		google.maps.event.addListener(marker, 'click', function(){  
			RtuInfowindow.close();
			infowindow.open(map,this);       
			RtuInfowindow = infowindow;
		}); */  
	});
}
/*function RemoveRtu(){
	for (var i = 0; i < RtuMarkers.length; i++) {
		RtuMarkers[i].setMap(null);
	}
}*/